//
//  FTHomePageHeaderView.m
//  项目2
//
//  Created by rimi on 16/8/20.
//  Copyright © 2016年 wzc. All rights reserved.
//

#import "FTHomePageHeaderView.h"
#import "ConfigDefine.h"
#import "ProjectViewController.h"
#import "UserState.h"
#import "MemberViewController.h"
#import "MakeWishViewController.h"
#import "ReHeatViewController.h"
#import "TodayFXViewController.h"
#import "TodayLuckyBagViewController.h"
#import "UserModel.h"
#import "WishListViewController.h"
#import <UIImageView+WebCache.h>


#define IMAGE_NUM 2
@interface FTHomePageHeaderView ()<UIScrollViewDelegate>



@end

@implementation FTHomePageHeaderView
@synthesize imageViews = _imageViews;

-(void)awakeFromNib{
    [super awakeFromNib];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    self.pageControl = pageControl;
    self.pageControl.numberOfPages = 1;
    [self addSubview:pageControl];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}




+(instancetype)loadHeaderView{
    FTHomePageHeaderView *vc = [[[NSBundle mainBundle]loadNibNamed:@"FTHomePageHeaderView" owner:nil options:nil]firstObject];
    return vc;
}
- (IBAction)todayFXBtnTaped:(id)sender {
    TodayFXViewController *vc = [[TodayFXViewController alloc]init];
    PLLOG(@"Today");
    [self.ownerVC.navigationController pushViewController:vc animated:YES];
}
- (IBAction)todayLuckyBagBtnTaped:(id)sender {
    TodayLuckyBagViewController *vc = [[TodayLuckyBagViewController alloc]init];
    [self.ownerVC.navigationController pushViewController:vc animated:YES];
}
- (IBAction)makeWishBtnTaped:(id)sender {
    UserModel *user = [UserModel sharedUserModel];
    if (user.islogin) {
        MakeWishViewController *vc = [[MakeWishViewController alloc]init];
        [self.ownerVC.navigationController pushViewController:vc animated:YES];
    }else{
        MemberViewController *vc = [[MemberViewController alloc]init];
        [self.ownerVC.navigationController pushViewController:vc animated:YES];
    }
    PLLOG(@"makeWish");
}
- (IBAction)checkWishBtnTaped:(id)sender {
    PLLOG(@"checkWishBtn");

    [self.ownerVC.tabBarController setSelectedIndex:1];

}
- (IBAction)reheatWishBtnTaped:(id)sender {
    PLLOG(@"reheatWishBtn");
    ReHeatViewController *vc = [[ReHeatViewController alloc]init];
    [self.ownerVC.navigationController pushViewController:vc animated:YES];
}
- (IBAction)myWishBtnTaped:(id)sender {
    PLLOG(@"myWishBtn");
    UserModel *user = [UserModel sharedUserModel];
    if (user.islogin) {
        WishListViewController *vc = [[WishListViewController alloc]init];
        [self.ownerVC.navigationController pushViewController:vc animated:YES];
    }else{
        MemberViewController *vc = [[MemberViewController alloc]init];
        [self.ownerVC.navigationController pushViewController:vc animated:YES];
    }
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.allLabel.layer.borderWidth = 2;
    self.allLabel.layer.borderColor = [UIColor redColor].CGColor;
    self.scrollView.contentSize = CGSizeMake((self.imageCount + 2) * self.imgW, 0);
    [self layoutImageViews];
    self.scrollView.frame = CGRectMake(0, 0, kScreenWidth, 160 * kScreenHeightScale);
    self.pageControl.frame = CGRectMake(self.scrollView.frame.size.width - 50, self.scrollView.frame.size.height - 10, 0, 0);
    self.imgW = self.scrollView.frame.size.width;
    self.imgH = self.scrollView.frame.size.height;
    self.view1.frame = CGRectMake(0, CGRectGetMaxY(self.scrollView.frame), kScreenWidth, 140 * kScreenHeightScale);
    [self.scrollView setContentOffset:CGPointMake(self.imgW, 0) animated:NO];
    self.view2.frame = CGRectMake(0, CGRectGetMaxY(self.view1.frame), kScreenWidth, 80);
//    self.frame = CGRectMake(0, 0, kScreenWidth, 320 * kScreenHeightScale);
}

- (void)nextImage{
    NSInteger page = self.pageControl.currentPage;
    page = self.pageControl.currentPage + 1;
    CGPoint offset = CGPointMake((1 + page) * self.imgW, 0);
    [self.scrollView setContentOffset:offset animated:YES];
}

- (void)setupImageViews{
    
    for (int i = 0; i < self.images.count + 2; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        CGFloat imageX = i * self.imgW;
        CGFloat imageY = 0;
        CGFloat imageW = self.imgW;
        CGFloat imageH = self.imgH;
        imageView.frame = CGRectMake(imageX, imageY, imageW,imageH);
        [self.scrollView insertSubview:imageView atIndex:0];
    }
    
}

- (NSArray *)imageViews{
    
    if (_imageViews == nil) {
        NSMutableArray *arr = [NSMutableArray array];
        for (int i = 0; i < self.images.count + 2; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            CGFloat imageX = i * self.imgW;
            CGFloat imageY = 0;
            CGFloat imageW = self.imgW;
            CGFloat imageH = self.imgH;
            imageView.frame = CGRectMake(imageX, imageY, imageW,imageH);
            [self.scrollView insertSubview:imageView atIndex:0];
            [arr addObject:imageView];
        }
        _imageViews = arr;
        
    }
    
    return _imageViews;
    
}

- (void)setImages:(NSArray *)images{
    
    _images = images;
    self.imageCount = images.count;
    self.pageControl.numberOfPages = self.imageCount;
    [self addPics];
    
}

- (void)layoutImageViews {
    for (int i = 0; i < _imageViews.count; i++) {
        UIImageView *imageView = _imageViews[i];
        imageView.backgroundColor = [UIColor redColor];
        CGFloat imageX = i * self.imgW;
        CGFloat imageY = 0;
        CGFloat imageW = self.imgW;
        CGFloat imageH = self.imgH;
        imageView.frame = CGRectMake(imageX, imageY, imageW,imageH);
    }
}

- (void)addPics{
    
    NSLog(@"image.count == %@",self.images);
    
    for (int i = 0; i < self.images.count + 2; i++) {
        UIImageView *imageView = self.imageViews[i];
        id image = nil;
        
        if (i == 0) {
            image = [self.images lastObject];
        }else if(i == self.images.count + 1){
            image = [self.images firstObject];
        }else{
            image = self.images[i - 1];
        }
        
        if ([image isKindOfClass:[UIImage class]]) {
            imageView.image = image;
        } else if ([image isKindOfClass:[NSURL class]]) {
           
            [imageView sd_setImageWithURL:image completed:nil];
        }
    }
    
}



- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self.timer invalidate];
    self.timer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop ] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView.contentOffset.x == self.imgW * (self.imageCount + 1)) {
        [self.scrollView setContentOffset:CGPointMake(self.imgW, 0) animated:NO];
    }else if(scrollView.contentOffset.x == 0){
        [self.scrollView setContentOffset:CGPointMake(self.imgW * (self.imageCount), 0) animated:NO];
    }
    
    self.pageControl.currentPage = (self.scrollView.contentOffset.x + self.imgW * 0.5f) / self.imgW - 1;
    
}






@end
